package com.bah.tract.client.ui;

import java.security.GeneralSecurityException;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Vector;
import java.util.prefs.Preferences;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.bah.tract.client.ErrorHandler;
import com.bah.tract.client.workstore.tol.TOLClientImpl;
import com.bah.tract.client.workstore.tol.TaxLocationProvider;
import com.bah.tract.security.PasswordEncryptor;
import com.google.inject.Inject;

public class TOLPreferencePanel extends PreferencePanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox taxLocationBox;
	private JPasswordField tolPassword;
	private JTextField tolUser;
	private final Preferences mPreferences;
	private final PasswordEncryptor mPasswordEncryptor;
	private final TaxLocationProvider mTaxLocationProvider;

	@Inject
	public TOLPreferencePanel(final Preferences preferences,
			final PasswordEncryptor passwordEncryptor,
			final TaxLocationProvider taxLocationProvider) {
		mPreferences = preferences;
		mPasswordEncryptor = passwordEncryptor;
		mTaxLocationProvider = taxLocationProvider;
		initComponents();
	}

	private void initComponents() {

		JLabel tolUserLbl = new javax.swing.JLabel();
		JLabel tolPasswordLbl = new javax.swing.JLabel();
		JLabel taxCodeLabel = new javax.swing.JLabel();
		taxLocationBox = new javax.swing.JComboBox();
		tolPassword = new javax.swing.JPasswordField();
		tolUser = new javax.swing.JTextField();

		final ResourceBundle resourceMap = ResourceBundle
				.getBundle("com.bah.tract.client.ui.PreferencesMenu");

		setName("jPanel1"); // NOI18N

		tolUserLbl.setText(resourceMap.getString("jLabel4.text")); // NOI18N
		tolUserLbl.setName("jLabel4"); // NOI18N

		tolPasswordLbl.setText(resourceMap.getString("jLabel5.text")); // NOI18N
		tolPasswordLbl.setName("jLabel5"); // NOI18N

		taxCodeLabel.setText(resourceMap.getString("jLabel6.text")); // NOI18N
		taxCodeLabel.setName("jLabel6"); // NOI18N
		final Vector<String> taxCodes = new Vector<String>(mTaxLocationProvider
				.getTaxLocations().keySet());
		String selected = null;
		final String taxCode = mPreferences.get("taxCode", null);
		taxLocationBox.setModel(new javax.swing.DefaultComboBoxModel(taxCodes));
		if (taxCode != null) {
			for (final Map.Entry<String, String> entry : mTaxLocationProvider
					.getTaxLocations().entrySet()) {
				if (entry.getValue().equals(taxCode)) {
					selected = entry.getKey();
				}
			}
			taxLocationBox.setSelectedItem(selected);

		}
		taxLocationBox.setName("jComboBox2"); // NOI18N

		final String encryptedPass = mPreferences.get(
				TOLClientImpl.TOL_PASS_PROP, null);
		String password;
		if (encryptedPass == null) {
			password = "password";
		} else {
			try {
				password = mPasswordEncryptor.decrypt(encryptedPass);
			} catch (final GeneralSecurityException e1) {
				password = "password";
			}
		}

		tolPassword.setText(password); // NOI18N
		tolPassword.setName("jPasswordField1"); // NOI18N
		final String tolUserId = mPreferences.get(TOLClientImpl.TOL_ID_PROP,
				"        ");
		tolUser.setText(tolUserId); // NOI18N

		tolUser.setName("jTextField1"); // NOI18N

		final javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				this);
		setLayout(jPanel1Layout);
		setBorder(BorderFactory.createTitledBorder("Time Online"));
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(25, 25, 25)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																taxCodeLabel)
														.addComponent(
																tolPasswordLbl)
														.addComponent(
																tolUserLbl))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																tolUser,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																tolPassword,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																taxLocationBox,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(226, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(19, 19, 19)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																tolUserLbl)
														.addComponent(
																tolUser,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																tolPasswordLbl)
														.addComponent(
																tolPassword,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																taxCodeLabel)
														.addComponent(
																taxLocationBox,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

	}

	@Override
	public final void savePreferences() {
		final String password = new String(tolPassword.getPassword());
		String encryptedPassword = null;
		try {
			encryptedPassword = mPasswordEncryptor.encrypt(password);
		} catch (final GeneralSecurityException e1) {
			ErrorHandler.handleError(e1);
		}
		mPreferences.put(TOLClientImpl.TOL_PASS_PROP, encryptedPassword);
		mPreferences.put(TOLClientImpl.TOL_ID_PROP, tolUser.getText().trim());
		final Object taxCodeSel = taxLocationBox.getSelectedItem();
		if (taxCodeSel != null) {
			final String taxCode = mTaxLocationProvider.getTaxLocations().get(
					taxLocationBox.getSelectedItem());
			mPreferences.put("taxCode", taxCode);
		}
	}

}
